package com.hyh.toyshopping.service.impl;

import com.hyh.toyshopping.mapper.CarMapper;
import com.hyh.toyshopping.mapper.OrderMapper;
import com.hyh.toyshopping.model.OrderDesc;
import com.hyh.toyshopping.model.Orders;
import com.hyh.toyshopping.model.view.OrderDescView;
import com.hyh.toyshopping.model.view.OrderView;
import com.hyh.toyshopping.service.OrderService;
import com.hyh.toyshopping.utils.OrderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @version 1.0
 * @BelongsProject:IntelliJ IDEA
 * @BelongsPackage:com.hyh.myshopping.service.Impl
 * @Author:hyhWTX
 * @ClassName:OrderServiceImpl
 * @CreateTime:2022年-12月-12日 16:35
 * @Description: 订单服务类
 */

@Service
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private CarMapper carMapper;

    @Override
    public void addOrder(Orders orders) {
        //获得自动生成订单编号,并设置到订单中
        orders.setOrder_id(OrderUtils.getOrderId());
        //向tbl_order表中添加订单信息
        orderMapper.addOrder(orders);

        int[] toy_ids = new int[orders.getOrderDescs().size()];
        int index = 0;
        //将orderId设置到orderDesc对象中order_id属性中
        for (OrderDesc orderDesc : orders.getOrderDescs()) {
            orderDesc.setOrder_id(orders.getOrder_id());
            toy_ids[index++] =orderDesc.getToy_id();


        }
        System.out.println(orders);
        //向tbl_order_desc表中添加订单明细
        orderMapper.addOrderDesc(orders.getOrderDescs());

        //从购物车中将购买的商品删除
        carMapper.deleteCarByUserIdAndToyId(orders.getUser_id(),toy_ids);
    }

    @Override
    public List<Orders> getOrderByUser(int user_id) {
        return orderMapper.getOrderByUser(user_id);
    }

    @Override
    public List<OrderDescView> getOrderDescByOrderId(String order_id) {
        return orderMapper.getOrderDescByOrderId(order_id);
    }

    @Override
    public List<OrderView> getOrderList() {
        return orderMapper.getOrderList();
    }

    @Override
    public void updateOrderStatus(String order_id, int order_status) {
        orderMapper.updateOrderStatus(order_id,order_status);
    }
}
